package com.burgeon.backend.EnterpriseWeChat.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.OrderVO.OrderEntityItem;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FindMentionOrder extends RESTBase {
    private String ip;
    private String account;
    private String password;

    public FindMentionOrder(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    public List<OrderEntityItem> getData(String store) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(store), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<OrderEntityItem> data = new ArrayList<>();
        for (int i = 0; i <jsonArray.length() ; i++) {

            OrderEntityItem orderEntityItem = new OrderEntityItem();
            orderEntityItem.setId(jsonArray.getJSONArray(i).getString(0));
            orderEntityItem.setUserName(jsonArray.getJSONArray(i).getString(1));
            orderEntityItem.setEndTime(jsonArray.getJSONArray(i).getString(2));
            orderEntityItem.setQTY(jsonArray.getJSONArray(i).getString(3));
            orderEntityItem.setXSNO(jsonArray.getJSONArray(i).getString(4));
            orderEntityItem.setSelfRasiNO(jsonArray.getJSONArray(i).getString(5));
            orderEntityItem.setStore(jsonArray.getJSONArray(i).getString(6));

            data.add(orderEntityItem);

        }

        return data;
    }

    /**
     * 准备向ERP传送字段拼接
     *
     * @return
     */
    private MultiValueMap Data(String store) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("'YC_RETAIL_ID;ID'");//ID
        columns.add("'YC_RETAIL_ID;RECEIVER_NAME'");//顾客姓名
        columns.add("STATUSTIME");//提货时间
        columns.add("'YC_RETAIL_ID;TOT_QTY'");//数量
        columns.add("'YC_RETAIL_ID;XSNO'");//线上单号
        columns.add("'YC_RETAIL_ID;SELFRAISNO'");//提货码
        columns.add("'C_STORE_ID;NAME'");//店仓

        params.put("columns", columns);
        params.put("table", "YC_RETAILOUT");//表名

        HashMap paramss = new HashMap();

        HashMap expr1 = new HashMap();
        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();

        expr1.put("combine","and");
        expr1v1.put("column","'YC_RETAIL_ID;STATUS'");
        expr1v1.put("condition","2");
        expr1.put("expr1",expr1v1);

        expr1v2.put("column","'YC_RETAIL_ID;IS_SELFRAIS'");
        expr1v2.put("condition","Y");
        expr1.put("expr2",expr1v2);

        HashMap expr2 = new HashMap();
        HashMap expr2v1 = new HashMap();
        HashMap expr2v2 = new HashMap();

        expr2.put("combine","and");
        expr2v1.put("column","'C_STORE_ID;NAME'");
        expr2v1.put("condition","'"+store+"'");
        expr2.put("expr1",expr2v1);

        expr2v2.put("column","STATUS");//提交状态
        expr2v2.put("condition","2");
        expr2.put("expr2",expr2v2);


        paramss.put("combine","and");
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);

        params.put("params", paramss);

        return DATA("Query", params, account, password);

    }

    public static void main(String[] args) {
        FindMentionOrder g = new FindMentionOrder("39.104.240.134:55321", "nea@burgeon.com.cn", "root55321");
        try {
            System.err.println(g.getData("会员礼品仓-杰秀嘉丽"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
